//
//  HLBCoverPhotoVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/12/5.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "HLBCoverPhotoVC.h"

@interface HLBCoverPhotoVC ()<UIActionSheetDelegate>{
    NSString *_imageUrl;
}
@property (weak, nonatomic) IBOutlet UIImageView *coverImage;

@end

@implementation HLBCoverPhotoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitle:@"编辑店头" titleColor:kBlackColor font:font17];
    [self rightNavItem];
    
    [_coverImage sd_setImageWithURL:[NSURL URLWithString:_editDataModel.album_cover] placeholderImage:[UIImage imageNamed:@"photo_bg12_default"]];
    _imageUrl = _editDataModel.album_cover;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];
    _coverImage.userInteractionEnabled = YES;
    _coverImage.contentMode = UIViewContentModeScaleAspectFit;
    [_coverImage addGestureRecognizer:tap];
    
    __weak typeof (self)weakself = self;
    [self.backBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        if([_imageUrl isEqualToString:_editDataModel.album_cover]){
            [weakself.navigationController popViewControllerAnimated:YES];
        }else{
            [weakself albumImgSaveAction];
        }
    }];

}

- (void)rightNavItem{
    
     __weak typeof (self)weakself = self;
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    [rightBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(UIButton* sender) {
        [weakself albumImgSaveAction];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
}



- (void)albumImgSaveAction{
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Album/albumImgSave" isactive:YES parameter:@{@"agent_album_id":_editDataModel.albumID,@"is_head":@"1",@"img_path":_editDataModel.album_cover} successBlock:^(PPHttpRequest *request, id responseObj) {
        
        [AlertUtils showToastWithMessage:@"保存成功" inView:nil hideAfterDelay:2.0 beIndeter:NO];
        [weakself.navigationController popViewControllerAnimated:YES];
        
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];

}

#pragma  mark ========选择相片========
- (void)chooseImage{
    UIActionSheet *sheet;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
    }else{
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
    }
    sheet.tag = 225;
    [sheet showInView:self.view];
}

#pragma  mark ========UIActionSheetDelegate========
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(actionSheet.tag == 225){
        NSUInteger sourceType = 0;
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            switch (buttonIndex) {
                case 0:
                    //取消
                    return;
                    break;
                case 1:
                    //相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 2:
                    //相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
        }else{
            if(buttonIndex==0){
                return;
            }else{
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.sourceType = sourceType;
        imagePickerController.navigationBar.translucent = NO;
        imagePickerController.allowsEditing = NO;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}

#pragma  mark ========UIImagePickerControllerDelegate========
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *newImg = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [[PPHttpRequsetData shareInstance] uploadImages:Request_POST URL:@"/ShopApi/Public/imgUpload" isactive:NO imageData:@[newImg] imgfileName:@"img_file" parameter:@{@"img_path":@"ALBUM/"} successBlock:^(PPHttpRequest *request, id responseObj) {
        
        [_coverImage sd_setImageWithURL:responseObj[@"url"] placeholderImage:nil];
        _editDataModel.album_cover = responseObj[@"url"];
       
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
